/**   
 * @Description 类功能描述
 * @Author: lvcai
 * @Create Date 2013-11-20 上午10:34:32
 * @Modified By:lvcai
 * @Modified Date:2013-11-20 上午10:34:32
 * @version v1.0
 * @Copyright(c)2013:东吴软件技术有限公司  
 */
package com.apache.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.apache.tools.ConfigUtil;

/**   
 * <p>@Description JDBC连接数据库
 * <p>@Copyright(c)2013:北京软件技术有限公司
 * <p>@Author: lvcai
 * <p>@Create Date 2013-11-20 上午10:34:32
 * <p>@Modified By:lvcai
 * <p>@Modified Date:2013-11-20 上午10:34:32
 * <p>@version v1.0  
 */
public class OracleConnection {

	private static Connection conn;

	/**
	 * 
	 * @return
	 * <p>@description 获取数据库连接
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-11-20 下午10:33:41
	 */
	public static Connection getConnec() {
		String ojdbcURL = ConfigUtil.getInstance().getValueByKey("application.properties", "jdbc.url");
		String ojdbcDriver = ConfigUtil.getInstance().getValueByKey("application.properties", "jdbc.driver");
		String userName = ConfigUtil.getInstance().getValueByKey("application.properties", "jdbc.username");
		String password = ConfigUtil.getInstance().getValueByKey("application.properties", "jdbc.password");
		if (conn != null) {
			return conn;
		}
		try {
			Class.forName(ojdbcDriver);
			conn = DriverManager.getConnection(ojdbcURL, userName, password);
			System.out.println("与oracle数据库连接成功...");
		} catch (Exception e) {
			System.out.println("与oracle数据库连接失败...");
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * 
	 * @param inputStream  文件流
	 * @param fileName  文件名称
	 * @throws Exception
	 * <p>@description clob对象修改
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-11-20 下午10:34:15
	 */
	public static void clobModify(File inputStream, String fileName) throws Exception {
		Connection conn = OracleConnection.getConnec();
		Statement stmt = conn.createStatement();
		/* 设定不自动提交 */
		boolean defaultCommit = conn.getAutoCommit();
		conn.setAutoCommit(false);
		try {
			/* 查询CLOB对象并锁定 */
			ResultSet rs = stmt.executeQuery("select blob_value_ from jbpm4_lob  where to_char(name_)='"
					+ fileName.trim() + "' for update");
			while (rs.next()) {
				/* 获取此CLOB对象 */
				oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("blob_value_");
				/* 进行覆盖式修改 */
				BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
				BufferedReader in = new BufferedReader(new FileReader(inputStream));
				int c;
				while ((c = in.read()) != -1) {
					out.write(c);
				}
				in.close();
				out.close();
			}

			/* 正式提交 */
			conn.commit();
		} catch (Exception ex) {
			/* 出错回滚 */
			conn.rollback();
			throw ex;
		}

		/* 恢复原提交状态 */

		conn.setAutoCommit(defaultCommit);

	}

	/**
	 * 
	 * @param inputStream  文件流
	 * @param fileName  文件名称
	 * @throws Exception
	 * <p>@description 覆盖流程图片流方法
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-11-20 下午10:49:52
	 */
	public static void blobReplace(File xmlFile, String xmlFileName, File picFile, String picFileName) throws Exception {
		Connection conn = OracleConnection.getConnec();
		Statement stmt = conn.createStatement();
		/* 设定不自动提交 */
		boolean defaultCommit = conn.getAutoCommit();
		conn.setAutoCommit(false);

		try {
			//1.处理流程定义文件 既xml文件
			/* 清空原BLOB对象 */
			stmt.executeUpdate("update jbpm4_lob set blob_value_=EMPTY_BLOB() where to_char(name_)='" + xmlFileName
					+ "'");
			/* 查询此BLOB对象并锁定 */
			ResultSet rs_xml = stmt.executeQuery("select blob_value_ from jbpm4_lob where to_char(name_)='"
					+ xmlFileName + "'");
			while (rs_xml.next()) {
				/* 取出此BLOB对象 */
				oracle.sql.BLOB blob_xml = (oracle.sql.BLOB) rs_xml.getBlob("blob_value_");
				/* 向BLOB对象中写入数据 */
				BufferedOutputStream out_xml = new BufferedOutputStream(blob_xml.getBinaryOutputStream());
				BufferedInputStream in_xml = new BufferedInputStream(new FileInputStream(xmlFile));
				int c;
				while ((c = in_xml.read()) != -1) {
					out_xml.write(c);
				}
				in_xml.close();
				out_xml.close();

			}

			//2.处理流程图文件  既png文件
			/* 清空原BLOB对象 */
			stmt.executeUpdate("update jbpm4_lob set blob_value_=EMPTY_BLOB() where to_char(name_)='" + picFileName
					+ "'");
			/* 查询此BLOB对象并锁定 */
			ResultSet rs_pic = stmt.executeQuery("select blob_value_ from jbpm4_lob where to_char(name_)='"
					+ picFileName + "'");
			while (rs_pic.next()) {
				/* 取出此BLOB对象 */
				oracle.sql.BLOB blob_pic = (oracle.sql.BLOB) rs_pic.getBlob("blob_value_");
				/* 向BLOB对象中写入数据 */
				BufferedOutputStream out_pic = new BufferedOutputStream(blob_pic.getBinaryOutputStream());
				BufferedInputStream in_pic = new BufferedInputStream(new FileInputStream(picFile));
				int cc;
				while ((cc = in_pic.read()) != -1) {
					out_pic.write(cc);
				}
				in_pic.close();
				out_pic.close();

			}
			/* 正式提交 */
			conn.commit();
		} catch (Exception ex) {
			/* 出错回滚 */
			conn.rollback();
			throw ex;
		}

		/* 恢复原提交状态 */
		conn.setAutoCommit(defaultCommit);
	}
}
